package chapter4XML;

import java.util.HashMap;
import java.util.Map;

// 计数器切面
public class Counter {
	
	private Map<Integer, Integer> count;
	
	public Counter() {
		count = new HashMap<>();
	}
	//切面方法
	public void itemSold(int itemId) {
		if(!count.containsKey(itemId)) {
			count.put(itemId, 1);
		}else {
			Integer temp = count.get(itemId);
			count.put(itemId, temp+1);
		}
	}
	
	public int getSales(int itemId) {
		return count.containsKey(itemId) ? count.get(itemId) : 0;
	}
	
	public void show() {
		for(Integer itemId : count.keySet()) {
			System.out.println(itemId + " has sold " + count.get(itemId));
		}
	}
}
